package codeTop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class q322 {
    public int coinChange(int[] coins, int amount) {
        List<Integer> list = new ArrayList<>();
        for (int coin : coins) {
            if (coin <= amount) {
                list.add(coin);
            }
        }
        Collections.sort(list);
        int[] dp = new int[amount+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        for (int i = 0; i <=amount; i++) {
            if (dp[i]!=-1){
                for (int coin : list) {
                    if (coin + i <= amount) {
                        if (dp[coin + i] == -1) {
                            dp[coin+i] = dp[i]+1;
                        }else {
                            dp[coin+i] = Math.min(dp[i]+1,dp[coin+i]);
                        }
                    }
                }
            }
        }
        return dp[amount];
    }

    public static void main(String[] args) {
        q322 q = new q322();
        System.out.println(q.coinChange(new int[]{2}, 3));
    }
}
